from django.shortcuts import render, redirect
from django.views.generic import View

from users.models import Site, Users
from votes.models import Votes


def UserTO(request):
    ip = request.META.get("REMOTE_ADDR")
    try:
        site = Site.objects.get(uip=ip)
        if site:
            uuid = site.uuid
            user = Users.objects.get(mobile=uuid)
            user.ip=ip
    except:
        user=None
    return user

# 首页数据展示与用户搜索的实现
class IndexView(View):
    def get(self,request):
        user = UserTO(request)
        users = Users.objects.filter(is_show = 1).order_by('-vote_num')
        users_list = []
        for inx,use in enumerate(users):
            if (inx+1)%4==0:
                use.inx=1
                users_list.append(use)
            else:
                users_list.append(use)
        context = {'user':user,'users':users_list}
        return render(request,'index.html',context)

    def post(self,request):
        user = UserTO(request)
        users_list = []
        fromdata_id = request.POST.get("userID")
        fromdata_name = request.POST.get("userNAME")
        if fromdata_id:
            try:
                userid = Users.objects.get(id=fromdata_id)
                users_list.append(userid)
            except:
                print("查询不在范围的用户")
        if fromdata_name:
            print(fromdata_name)
            user_data = Users.objects.filter(user_name=fromdata_name.strip()).order_by('-vote_num')
            print(user_data)
            for inx, use in enumerate(user_data):
                if (inx + 1) % 4 == 0:
                    use.inx = 1
                    users_list.append(use)
                else:
                    users_list.append(use)
        context = {'user':user,'users':users_list}
        # {'user': user, 'user_agent': user_agent, 'ip': ip, }
        return render(request,'index.html',context)

# 用户登录与退出
class HomeView(View):
    def get(self,request):
        ip = request.META.get("REMOTE_ADDR")
        site = Site.objects.get(uip=ip)
        site.delete()
        return redirect('/home/index')

    def post(self,request):
        ip = request.META.get("REMOTE_ADDR")
        data = request.POST
        mobile = data.get('mobile')
        password = data.get('password')
        user = Users.objects.get(mobile=mobile)
        if user.password==password:
            try:
                site = Site.objects.get(uip=ip)
                site.delete()
            except:
                pass
            site  = Site.objects.create(
                    uuid = mobile,
                    uip = ip)
        else:
            user = None
        return redirect('/home/index')

# 用户注册与投票实现
class RegisterView(View):
    def post(self,request):
        ip = request.META.get("REMOTE_ADDR")
        data = request.POST
        name = data.get('name')
        mobile = data.get('mobile')
        password = data.get('password')
        try:
            site = Site.objects.get(uip=ip)
            site.delete()
        except:
            pass
        try:
            user = Users.objects.get(mobile=mobile)
        except:
            Users.objects.create(
                mobile=mobile,
                password=password,
                user_name=name)
            Site.objects.create(
                    uuid=mobile,
                    uip=ip)
        return redirect('/home/index')

    def get(self,request):
        user = UserTO(request)
        id = request.GET.get('id')
        try:
            userQ = Votes.objects.get(voteID=user.id)
            return redirect('/home/rule')
        except:
            pass
        try:
            userBy = Users.objects.get(id = id)
            print(userBy.vote_num)
            userBy.vote_num +=1
            print(userBy.vote_num)
            userBy.save()
            Votes.objects.create(
                voteID=user.id,
                voteByID=userBy.id,
                Ip=user.ip)
            return redirect('/home/list')
        except:
            return redirect('/home/index')
# 活动介绍
class IntroduceView(View):
    def get(self,request):
        # user = User()
        # user.avatar_url = '/static/images/2.jpg'
        # user.nick = '知识就是力量'
        user = None
        context = {'user':user}
        return render(request,'introduce.html',context)
# 活动规则与选手信息
class RuleView(View):
    def get(self,request):
        id = request.GET.get('id')
        user = UserTO(request)
        userid = None
        if id:
            userid = Users.objects.get(id=id)
        context = {'user': user, 'users': userid}
        return render(request, 'rule.html', context)
# 官网公告
class AfficheView(View):
    def get(self,request):
        # user = User()
        # user.avatar_url = '/static/images/2.jpg'
        # user.nick = '知识就是力量'
        user = None
        context = {'user':user}
        return render(request,'affiche.html',context)
# 排行
class ListView(View):
    def get(self,request):
        user = UserTO(request)
        users = Users.objects.filter(is_show=1).order_by('-vote_num')
        users_list = []
        for inx, use in enumerate(users):
            if (inx + 1) % 4 == 0:
                use.inx = 1
                users_list.append(use)
            else:
                users_list.append(use)
        context = {'user': user, 'users': users_list}
        return render(request, 'list.html', context)
# 个人中心
class PersonalView(View):
    def get(self,request):
        user = UserTO(request)
        context = {'user':user,'users':user}
        return render(request,'personal.html',context)
    # def post(self,request):
    #     user = UserTO(request)
    #     if user:
    #         Name = request.POST.get('Name')
    #         image = request.body
    #         user.user_name = Name
    #         image_path = "/static/images/%08d.jpg" % (user.id)
    #         user.image_path = image_path
    #         user.save()
    #         with open(image_path, "wb") as f:
    #            f.write(image)
    #         return redirect('/home/perso')
    #     else:
    #         return redirect('/home/index')